package com.aaa.util;

import com.fasterxml.jackson.core.io.IOContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
@Component
@Slf4j
public class FileUtil {
    public static String upload(MultipartFile photo, String savePath) throws IOException{
      String originalFilename = photo.getOriginalFilename();
        //非空判断
        String i="";
        if (!"".equals(originalFilename)) {
            //删除文件名/以后的字符
            String substring = photo.getContentType().substring(0, photo.getContentType().indexOf("/"));
            log.info(substring);
            //判断是否image
            if (substring.equals("image")) {
                //重命名：防止文件名称重复覆盖
                File file = new File(savePath + originalFilename);
                if (!file.exists()) {
                    boolean mkdir = file.mkdirs();
                    log.info(String.valueOf(mkdir));
                }
                i += originalFilename;
                //另存为
                photo.transferTo(file);
            }
            //上传成功返回
        }
        return i;
    }
    public static String[] multupload(MultipartFile[] photos, String savePath) throws IOException{
        String[] savePaths=new String[photos.length];
          for(int i=0;i<photos.length;i++){
              savePaths[i]=upload(photos[i],savePath);
          }
          return savePaths;
    }
}
